package team.star.werespos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.star.werespos.mapper.FoodInformationMapper;
import team.star.werespos.exception.GlobalException;
import team.star.werespos.model.entity.FoodInformation;
import team.star.werespos.model.enums.CodeMsgEnum;
import team.star.werespos.service.FoodInformationService;

import java.util.List;
import java.util.Optional;

/**
 * FoodInformationServiceImpl class<br/>
 *
 * @author CAI
 * @since 2019/03/25
 */
@Slf4j
@Service
public class FoodInformationServiceImpl implements FoodInformationService {
    @Autowired
    private FoodInformationMapper foodInformationMapper;

    @Override
    public void insertOne(FoodInformation foodInformation) {
        // 非空检查
        Preconditions.checkNotNull(foodInformation, "foodInformation is null!");
        // 新增
        Optional.ofNullable(foodInformationMapper.insert(foodInformation))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【新增商品信息】失败！result = [{}], foodInformation = [{}]",
                                result, foodInformation);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }

    @Override
    public List<FoodInformation> listFood(Integer categoryId) {
        QueryWrapper<FoodInformation> queryWrapper = new QueryWrapper<FoodInformation>().orderByAsc("create_time");
        if (categoryId != null) {
            queryWrapper.eq("category_id", categoryId);
        }
        return foodInformationMapper.selectList(queryWrapper);
    }

    @Override
    public List<FoodInformation> listFood(List<Integer> foodIds) {
        List<FoodInformation> foodList = foodInformationMapper.selectBatchIds(foodIds);
        if (foodList.size() != foodIds.size()) {
            log.error("【菜品列表】foodIds = {}, CodeMsgEnum.FOOD_NOT_EXIST = {}", foodIds, CodeMsgEnum.FOOD_NOT_EXIST);
            throw GlobalException.create(CodeMsgEnum.FOOD_NOT_EXIST);
        }
        return foodList;
    }

    @Override
    public void updateById(FoodInformation foodInformation) {
        Optional.ofNullable(foodInformationMapper.updateById(foodInformation))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【修改商品信息】失败！result = [{}], foodInformation = [{}]",
                                result, foodInformation);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }

    @Override
    public void deleteById(FoodInformation foodInformation) {
        Optional.ofNullable(foodInformationMapper.deleteById(foodInformation))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【删除商品信息】失败！result = [{}], foodInformation = [{}]",
                                result, foodInformation);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }

    @Override
    public FoodInformation getOne(Integer foodId) {
        FoodInformation foodInformation = foodInformationMapper.selectById(foodId);
        if (foodInformation == null) {
            log.error("【查看商品信息】foodId = {}, CodeMsgEnum.FOOD_NOT_EXIST = {}", foodId, CodeMsgEnum.FOOD_NOT_EXIST);
            throw GlobalException.create(CodeMsgEnum.FOOD_NOT_EXIST);
        }
        return foodInformation;
    }

}
